package com.tree.leetcode.array;

import cn.hutool.json.JSONUtil;

import java.util.Stack;

/**
 * @ClassName com.tree.leetcode.array
 * Description: LCR 038. 每日温度
 * <p>
请根据每日 气温 列表 temperatures ，重新生成一个列表，要求其对应位置的输出为：要想观测到更高的气温，至少需要等待的天数。如果气温在这之后都不会升高，请在该位置用 0 来代替。

示例 1:
输入: temperatures = [73,74,75,71,69,72,76,73]
输出: [1,1,4,2,1,1,0,0]
示例 2:
输入: temperatures = [30,40,50,60]
输出: [1,1,1,0]
示例 3:

输入: temperatures = [30,60,90]
输出: [1,1,0]

 * </p>
 * @Author tree
 * @Date 3/16/24 2:09 PM
 * @Version 1.0
 */
public class DailyTemperatures_038 {
    public static void main(String[] args) {
        int[] temperatures = {73,74,75,71,69,72,76,73};
        System.out.println(JSONUtil.toJsonStr(dailyTemperatures(temperatures)));
    }

    public static int[] dailyTemperatures(int[] temperatures) {
        Stack<Integer> stack = new Stack();
        stack.push(0);
        int[] ans = new int[temperatures.length];
        for(int i = 1; i < temperatures.length; i++){
            int top = stack.peek();
            if(temperatures[i] <= temperatures[top]){
                stack.push(i);
            }else{
                System.out.println("i= "+i+",top="+top +",peek"+stack.peek()+", 当前栈="+JSONUtil.toJsonStr(stack));
                while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]){
                    int topStack = stack.peek();
                    ans[topStack] = i - topStack;
                    stack.pop();
                }
                stack.push(i);
            }

        }
        return ans;

    }
}
